package birdsong.filter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class User {

	private String fromNickname;

	private Map<String,Long> message;
	private long time;
	private int msgSpam;
	private int userSpam;

	/**
	 * Constructer for the user class
	 * @param fromNickname
	 */
	public User(String fromNickname) {

		this.fromNickname = fromNickname;
		message = new HashMap<String, Long>();
		msgSpam=1;
		userSpam=1;
	}
/**
 * Getter method for the fromNickname field
 * @return fromNickname
 */
	public String getFromNickname() {
		return fromNickname;
	}
	
/**
 * Getter method for the message
 * @return message
 */
	public Map<String, Long> getMessage() {
		return message;
	}

/**
 * Getter method for the msgSpam counter
 * @return msgSpam counter for the number of times an user sent the same message
 */
	public int getMsgSpam() {
		return msgSpam;
	}
	/**
	 * Getter method for the userSpam counter
	 * @return userSpam counter for the number of times an user sent the a message
	 */
	public int getUserSpam() {
		return userSpam;
	}

	/**
	 * Method that increments the msgSpam counter
	 */
	public void incrementMsgSpam(){
		msgSpam++;
	}
	/**
	 * Method that increments the userSpam counter
	 */
	public void incrementUserSpam(){
		userSpam++;
	}
	/**
	 * Method that resets the msgSpam counter
	 */
	public void resetMsgSpam() {
		msgSpam = 1;
	}
	/**
	 * Method that reset the userSpam counter
	 */
	public void resetUserSpam(){
		userSpam = 1;
	}
	/**
	 * Method that adds a message to a speciefied user
	 */
	public void addMsg(String m, long timeReceived){
		message.put(m, timeReceived);
	}
/**
 * Method that checks if a message was sent by the user
 * @param birdsongMessage message
 * @param timeReceived time when the method was called
 * @return
 */
	public boolean hasMsg(String birdsongMessage, long timeReceived) {
		
        Set s=message.entrySet();

        Iterator it=s.iterator();

        while(it.hasNext())
        {
         
            Map.Entry m =(Map.Entry)it.next();

            if(m.getKey().equals(birdsongMessage)){
            	if(((Long) m.getValue()-timeReceived)<30000){
            		return true;
            	}
            }
        }
		return false;	
	}
}
